/*
 * timer_counter.c
 *
 *  Created on: Oct 24, 2013
 *      Author: Ken Arok
 */

#include "timer_counter.h"

volatile uint32_t TimersArray500ms[32];
volatile uint32_t TimersArray10ms[32];
TimerFuncVoid_t FuncArray500ms[32];
TimerFuncVoid_t FuncArray10ms[32];
void *ParamFuncArray500ms[32];
void *ParamFuncArray10ms[32];

/* Timer mask bit. */
static uint32_t func500MaskBit = 0x00000000;
static uint32_t func10MaskBit = 0x00000000;
static uint32_t tmr500MaskBit = 0x00000000;
static uint32_t tmr10MaskBit = 0x00000000;


void vTimer500msFuncReg(TimerFuncVoid_t _func, void *_param)
{
	uint8_t i, _exit;
	uint32_t _mask;

	i = 0;
	_exit = 1;
	_mask = 0x00000001;
	do {
		if(!(func500MaskBit & _mask)) {
			func500MaskBit |= _mask ;
			FuncArray500ms[i] = _func;
			ParamFuncArray500ms[i] = _param;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 32));
}


void vTimer500msFuncUnreg(TimerFuncVoid_t _func)
{
	uint8_t i, _exit;
	uint32_t _mask;

	i = 0;
	_exit = 1;
	_mask = 0x00000001;
	do {
		if(_func == FuncArray500ms[i]) {
			func500MaskBit &= ~_mask;
			FuncArray500ms[i] = NULL;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 32));
}


void vTimer10msFuncReg(TimerFuncVoid_t _func, void *_param)
{
	uint8_t i, _exit;
	uint32_t _mask;

	i = 0;
	_exit = 1;
	_mask = 0x00000001;
	do {
		if(!(func10MaskBit & _mask)) {
			func10MaskBit |= _mask ;
			FuncArray10ms[i] = _func;
			ParamFuncArray10ms[i] = _param;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 32));
}


void vTimer10msFuncUnreg(TimerFuncVoid_t _func)
{
	uint8_t i, _exit;
	uint32_t _mask;

	i = 0;
	_exit = 1;
	_mask = 0x00000001;
	do {
		if(_func == FuncArray10ms[i]) {
			func10MaskBit &= ~_mask;
			FuncArray10ms[i] = NULL;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 32));
}


volatile void *vTimer10msTake(void)
{
	uint8_t i, _exit;
	uint32_t _mask;

	i = 0;
	_exit = 1;
	_mask = 0x00000001;
	do {
		if(!(tmr10MaskBit & _mask)) {
			tmr10MaskBit |= _mask ;
			_exit = 0;
			return &TimersArray10ms[i];
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 32));

	return NULL;
}


void vTimer10msGive(volatile void *_tmrAddr)
{
	uint8_t i, _exit;
	uint32_t _mask;

	i = 0;
	_exit = 1;
	_mask = 0x00000001;
	do {
		if(_tmrAddr == &TimersArray10ms[i]) {
			tmr10MaskBit &= ~_mask;
			_tmrAddr = NULL;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 32));
}


volatile void *vTimer500msTake(void)
{
	uint8_t i, _exit;
	uint32_t _mask;

	i = 0;
	_exit = 1;
	_mask = 0x00000001;
	do {
		if(!(tmr500MaskBit & _mask)) {
			tmr500MaskBit |= _mask ;
			_exit = 0;
			return &TimersArray500ms[i];
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 32));

	return NULL;
}



void vTimer500msGive(volatile void *_tmrAddr)
{
	uint8_t i, _exit;
	uint32_t _mask;

	i = 0;
	_exit = 1;
	_mask = 0x00000001;
	do {
		if(_tmrAddr == &TimersArray500ms[i]) {
			tmr500MaskBit &= ~_mask;
			_tmrAddr = NULL;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 32));
}
